package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.domain.po.Student;
import com.itheima.domain.query.StudentQueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.domain.vo.Result;
import com.itheima.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "学生管理", description = "学生管理相关接口")
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Operation(summary = "分页查询学生", description = "根据条件分页查询学生列表")
    @GetMapping
    public Result list(@Parameter(description = "查询参数") StudentQueryParam student) {
        log.info("查询所有学生信息：{}", student);
        PageResult pageResult = studentService.studentPage(student);
        return Result.success(pageResult);
    }

    @Operation(summary = "新增学生", description = "新增学生信息")
    @Log
    @PostMapping
    public Result save(@Parameter(description = "学生信息") @RequestBody Student student) {
        log.info("新增学生信息：{}", student);
        studentService.saveStudent(student);
        return Result.success();
    }

    @Operation(summary = "根据ID查询学生", description = "根据学生ID查询学生详细信息")
    @GetMapping("/{id}")
    public Result getById(@Parameter(description = "学生ID") @PathVariable Integer id) {
        log.info("根据id查询学生信息：{}", id);
        Student studentById = studentService.getById(id);
        return Result.success(studentById);
    }

    @Operation(summary = "修改学生", description = "更新学生信息")
    @Log
    @PutMapping
    public Result update(@Parameter(description = "学生信息") @RequestBody Student student) {
        log.info("修改学生信息：{}", student);
        studentService.updateStudent(student);
        return Result.success();
    }

    @Operation(summary = "批量删除学生", description = "根据ID列表批量删除学生")
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@Parameter(description = "学生ID列表") @PathVariable List<Integer> ids) {
        log.info("根据id删除学生信息：{}", ids);
        return Result.success(studentService.removeByIds(ids));
    }

    @Operation(summary = "学生违纪扣分", description = "根据学生ID进行违纪扣分")
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@Parameter(description = "学生ID") @PathVariable Integer id,
            @Parameter(description = "扣分数") @PathVariable Integer score) {
        log.info("根据id{}扣分, 扣分：{}", id, score);
        studentService.updateViolatuion(id, score);
        return Result.success();
    }

}
